<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment news model
 * @since 1.0
 */
class news_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->library('common');
    }

    /**
     * get count news
     */
    function get_count($cat = 0) {
        if ($cat == 0) {
            return $this->db->count_all('news');
        } else {
            $this->db->where('category_id', $cat);
            $this->db->from('news');
            return $this->db->count_all_results();
        }
    }

    /**
     * get news by param
     * @param int $id
     * @param int $limit
     * @param int $offset
     * @return null
     */
    function get($id = -1, $limit = -1, $offset = 0, $order_by = 'date', $order_type = 'desc', $cat_id = 0) {
        if ($id == -1) {
            if ($limit == -1) {
                if ($cat_id > 0) {
                    $this->db->where('category_id', $cat_id);
                }
                $this->db->order_by($order_by, $order_type);
                $query = $this->db->get('news');
            } else {
                if ($cat_id > 0) {
                    $this->db->where('category_id', $cat_id);
                }
                $this->db->order_by($order_by, $order_type);
                $query = $this->db->get('news', $limit, $offset);
            }

            $result = $query->result();
            return $result;
        } else if ($id > 0) {
            $query = $this->db->get_where('news', array('id' => $id));
            $result = $query->result();
            if (count($result) > 0) {
                return $result[0];
            } else {
                return null;
            }
        }
    }

    /**
     * insert a news to database
     * @param type $title
     * @param type $description
     * @param type $content
     * @param type $image
     * @param type $cat_id
     * @param type $permalink 
     * @return boolean
     */
    function add($title, $description, $content, $image, $cat_id, $permalink) {
        $news = array(
            'title' => $title,
            'description' => $description,
            'content' => $content,
            'image' => $image,
            'category_id' => $cat_id,
            'date' => date('Y/m/d'),
            'permalink' => $permalink
        );
        $this->db->insert('news', $news);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * update news to database
     * @param int $id
     * @param string $title
     * @param string $description
     * @param string $content
     * @param string $image
     * @param int $cat_id
     * @return boolean
     */
    function edit($id, $title, $description, $content, $image, $cat_id) {
        $news = array(
            'title' => $title,
            'description' => $description,
            'content' => $content,
            'image' => $image,
            'category_id' => $cat_id
        );
        $this->db->where('id', $id);
        $this->db->update('news', $news);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * delete a news from database
     * @param int $id
     */
    function delete($id = 0) {
        $this->db->delete('news', array('id' => $id));
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 
     * @param type $str
     */
    function search($str = '') {
        $this->db->like('title', $str);
        $query = $this->db->get('news');
        $result = $query->result();

        return $result;
    }

    /**
     * generate permalink for news
     * @param string $str name of a news
     * @param type $max_length max length of link
     * @return string permalink
     */
    function generate_permalink($str, $max_length = -1) {
        $str = strtolower($str);
        if ($max_length > 0) {
            if (strlen($str) > $max_length) {
                $link = $this->truncateString_($str, $max_length);
            }
            else
                $link = $str;
        }
        else {
            $link = $str;
        }

        $link = $this->common->removesign($str);
        $link = $this->common->remove_disallow($link);
        $link = $this->common->removespace($link);
        $link = urlencode($link);

        if ($this->check_permalink($link) == false) {
            return $link;
        }
        $i = 0;
        while (1) {
            $i++;
            $link_temp = $link . '(' . $i . ')';
            if ($this->check_permalink($link_temp) == false)
                return $link_temp;
        }
    }

    function get_by_permalink($str) {
        $query = $this->db->get_where('news', array('permalink' => $str));
        $result = $query->result();
        if (count($result) > 0) {
            return $result[0];
        } else {
            return null;
        }
    }

    function check_permalink($str) {
        $news = $this->get_by_permalink($str);
        if (isset($news) && $news != NULL) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>
